package com.zygame.sdk.model;

import java.util.ArrayList;
import java.util.Observable;

import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;

public class UserModel extends Observable{
	private static UserModel model;

	public static UserModel getInstance() {
		if (model == null) {
			model = new UserModel();
		}
		return model;
	}

	public void setUserModel(String userJson) {
		UserModel userModel = null;

		if (TextUtils.isEmpty(userJson.trim())) {
			userModel = new UserModel();
		} else {
			try {
				userModel = new UserModel(new JSONObject(userJson));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.model = userModel;
	}

	public void clearUser() {
		 model = null;
//		model = new UserModel();
	}

	public boolean isLogin() {
		return !TextUtils.isEmpty(model.login_Token);
	}

	private ArrayList<HistoryGameServer> historys = new ArrayList<UserModel.HistoryGameServer>();;

	private int code;
	private int is_adult;
	private int vip;
	private int phone_bind;
	private int coin;
	private int point;
	private int vip_deep;
	private int vip_exp;
	private int vip_upper;
	private String login_account;
	private String last_login_ip;
	private String last_login_time;
	private String header;
	private String msg;
	private String login_Token;
	private String deviceno;
	private String pursecoin;
	private String uid;

	public String getName() {
		return name;
	}

	private String name ;

	class HistoryGameServer {
		private String game_pic;
		private String game_name;
		private String game_key;
		private String game_id;
		private String sid;
		private String server_name;
		private String server_url;  
		private String hotsort;

		@Override
		public String toString() {
			return "HistoryGameServer [game_pic=" + game_pic + ", game_name="
					+ game_name + ", game_key=" + game_key + ", game_id="
					+ game_id + ", sid=" + sid + ", server_name=" + server_name
					+ ", server_url=" + server_url + ", hotsort=" + hotsort
					+ "]";
		}

	}

	public UserModel() {
		super();
		// TODO Auto-generated constructor stub
	}   

	public UserModel(JSONObject jsonObject) {
		Builder(jsonObject);
	}
	
	
	public UserModel(String userName) {
		this.login_account = userName; 
	}
	
	public void setJson(String json){
		if (model == null) {
			model = new UserModel();
		}
	}
	
	

	public UserModel Builder(JSONObject jsonObject) {
		this.uid = jsonObject.optString("uid");
		this.code = jsonObject.optInt("code");
		this.login_account = jsonObject.optString("user_name");
		this.login_Token = jsonObject.optString("login_token");
		this.msg = jsonObject.optString("message");
		/*
		this.code = jsonObject.optInt("code");  
		JSONObject dataObject = jsonObject.optJSONObject("data");
		this.login_account = dataObject.optString("LOGIN_ACCOUNT");
		this.is_adult = dataObject.optInt("IS_ADULT");
		this.last_login_ip = dataObject.optString("LAST_LOGIN_IP");
		this.last_login_time = dataObject.optString("LAST_LOGIN_TIME");
		JSONArray historyArray = dataObject.optJSONArray("HISTORY_GAME_SERVER");

		int length;

		if (historyArray == null || (length = historyArray.length()) == 0) {
		} else {
			for (int i = 0; i < length; i++) {
				HistoryGameServer gameServer = new HistoryGameServer();
				JSONObject object = historyArray.optJSONObject(i);
				gameServer.game_pic = object.optString("GAME_PIC");
				gameServer.game_name = object.optString("GAME_NAME");
				gameServer.game_key = object.optString("GAME_KEY");
				gameServer.game_id = object.optString("GAME_ID");
				gameServer.sid = object.optString("SID");
				gameServer.server_name = object.optString("SERVER_NAME");
				gameServer.server_url = object.optString("SERVER_URL");
				gameServer.hotsort = object.optString("HOTSORT");
				this.historys.add(gameServer);
			}
		}

		this.vip = dataObject.optInt("VIP");
		this.phone_bind = dataObject.optInt("PHONE_BIND");
		this.coin = dataObject.optInt("COIN");
		this.point = dataObject.optInt("POINT");
		this.vip_deep = dataObject.optInt("VIP_DEEP");
		this.vip_exp = dataObject.optInt("VIP_EXP");
		this.vip_upper = dataObject.optInt("VIP_UPPER");
		this.header = dataObject.optString("HEADER");
		this.msg = dataObject.optString("msg");
		this.token = dataObject.optString("token");
		this.deviceno = dataObject.optString("deviceno");
		this.pursecoin = dataObject.optInt("PURSECOIN")+"";
		this.uid = dataObject.optString("uid");*/
		return this;
	}
	
	public static UserModel getModel() {
		return model;
	}

	public int getCode() {
		return code;
	}

	public String getLogin_account() {
		return login_account;
	}

	public int getIs_adult() {
		return is_adult;
	}

	public String getLast_login_ip() {
		return last_login_ip;
	}

	public String getLast_login_time() {
		return last_login_time;
	}

	public ArrayList<HistoryGameServer> getHistorys() {
		return historys;
	}

	public int getVip() {
		return vip;
	}

	public int getPhone_bind() {
		return phone_bind;
	}
	
	
	public void setPhone_bind(int phone_bind) {
		this.phone_bind = phone_bind;
		setChanged();
		super.notifyObservers();
	}

	public int getCoin() {
		return coin;
	}

	public int getPoint() {
		return point;
	}

	public int getVip_deep() {
		return vip_deep;
	}

	public int getVip_exp() {
		return vip_exp;
	}

	public int getVip_upper() {
		return vip_upper;
	}

	public String getHeader() {
		return header;
	}

	public String getMsg() {
		return msg;
	}

	public String getLogin_Token() {
		return login_Token;
	}
  
//	public String getDeviceno() {
//		return deviceno;
//	}
	
	public void setPursecoin(String pursecoin) {
		this.pursecoin = pursecoin;
		setChanged();
		super.notifyObservers();
	}

	public String getPursecoin(){
		return pursecoin;
	}
	
	public String getUid(){
		return uid;
	}
	
	@Override
	public String toString() {
		return "UserModel [code=" + code + ", login_account=" + login_account
				+ ", is_adult=" + is_adult + ", last_login_ip=" + last_login_ip
				+ ", last_login_time=" + last_login_time + ", historys="
				+ historys + ", vip=" + vip + ", phone_bind=" + phone_bind
				+ ", coin=" + coin + ", point=" + point + ", vip_deep="
				+ vip_deep + ", vip_exp=" + vip_exp + ", vip_upper="
				+ vip_upper + ", header=" + header + ", msg=" + msg
				+ ", token=" + login_Token + "]";
	}
}
